package Reeks5.VanRansbeeck.Kevin.View;

import Reeks5.VanRansbeeck.Kevin.Util.Settings;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NaamScreen extends CenteredJDialog {
    public NaamScreen(final SplashScreen f) {
        super(Settings.lang.getEnterName()+":");
        final JButton okay = new JButton("OK");
        final JButton cancel = new JButton("Cancel");
        final JTextField tf = new JTextField(16);
        JPanel panel = new JPanel();
        panel.add(tf);
        
        KeyListener kl = new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                //System.out.println("Pressed:" + e.getKeyCode() + " - " + e.getSource());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //System.out.println("Released:" + e.getKeyCode() + " - " + e.getSource());
                //System.out.println(tf.getText());
                if(e.getKeyCode() == 10) {
                    if(e.getSource() == cancel) {
                        System.exit(0);
                    }
                    else if(tf.getText().length() > 3) {
                        f.naam = tf.getText();
                        f.redraw();
                        dispose();
                    }
                }
                else if(e.getKeyCode() == 32) {
                    if(e.getSource() == cancel) {
                        System.exit(0);
                    } else if(e.getSource() == okay) {
                        f.naam = tf.getText();
                        f.redraw();
                        dispose();
                    }
                }
                else if (e.getKeyCode() == 27) {
                    System.exit(0);
                }
            }
        };
        
        tf.addKeyListener(kl);
        okay.addKeyListener(kl);
        cancel.addKeyListener(kl);
        addKeyListener(kl);
        
        ActionListener l = new ActionListener()
         {
             public void actionPerformed(ActionEvent e)
             {
                 JButton button = (JButton)e.getSource();
                 if(button == okay && tf.getText().length() > 2) {
                     f.naam = tf.getText();
                     f.redraw();
                    dispose();
                 } 
                 if(button == cancel) {
                     System.exit(0);
                 }
             }
         };
         okay.addActionListener(l);
         cancel.addActionListener(l);
         
         JPanel south = new JPanel();
         south.add(okay);
         south.add(cancel);
         getContentPane().add(panel);
         getContentPane().add(south, BorderLayout.SOUTH);
         setSize(250,100);
         setLocationRelativeTo(f.getParent());
         setVisible(true);
    }
}